#include "BlockQueue.hpp"
#include "Task.hpp"
#include <ctime>
#include <unistd.h>
#include <functional>

using namespace std;

//生产者
void* Productor(void* args)
{
    //BlockQueue<int>* bq = static_cast<BlockQueue<int>*>(args);
    //BlockQueue<CalTask>* bq = static_cast<BlockQueue<CalTask>*>(args);
    BlockQueue<CalTask>* cbq = static_cast<BlockQueues<CalTask, SaveTask>*>(args)->_cbq;

    while(1)
    {
        // 开始生产
        // int in = rand() % 10 + 1; // 1~10
        // bq->push(in);

        int x = rand() % 10;
        int y = rand() % 12;
        int op = operations[rand() % operations.size()];
        CalTask t(x, y, op, MyMath);
        cbq->push(t);
    
        //打印生产的任务
        std::cout << "productor 生产了任务："<< t.toTastString() << std::endl;


        //cout << "生产数据data：" << in << endl;

        
    }

    return nullptr;

}

//消费者
void* Consumer(void* args)
{
    //BlockQueue<int>* bq = static_cast<BlockQueue<int>*>(args);


    //BlockQueue<CalTask>* bq = static_cast<BlockQueue<CalTask>*>(args);
    BlockQueue<CalTask>* cbq = static_cast<BlockQueues<CalTask, SaveTask>*>(args)->_cbq;
    BlockQueue<SaveTask>* sbq = static_cast<BlockQueues<CalTask, SaveTask>*>(args)->_sbq;

    while(1)
    {
        // 开始消费
        // int out;
        // bq->pop(&out);

        // cout << "消费数据data：" << out << endl;
        //sleep(1);


        CalTask t;
        cbq->pop(&t);
        std::string result = t();
        std::cout << "consumer thread 消费了任务" << result << std::endl;


        //推送数据
        SaveTask s(result, Save);
        sbq->push(s);
        std::cout << "consumer thread 推送数据....." << std::endl;

        //cout << "消费数据" << t() << endl;
    }
    return nullptr;
}


void* Saver(void* args)
{


    BlockQueue<SaveTask>* sbq = static_cast<BlockQueues<CalTask, SaveTask>*>(args)->_sbq;

    while(1)
    {
        SaveTask s;
        sbq->pop(&s); 

        s();
        cout << "saver thread 存储数据成功" << std::endl;
        sleep(1);
    }
    return nullptr;
}


int main()
{
    srand((unsigned int)time(nullptr));

    // BlockQueue<CalTask>* bq = new BlockQueue<CalTask>;
    BlockQueues<CalTask, SaveTask> bqs;
    bqs._cbq = new BlockQueue<CalTask>();
    bqs._sbq = new BlockQueue<SaveTask>();

    pthread_t c, p;
    pthread_t s;
    
    pthread_create(&c, nullptr, Consumer, &bqs);
    pthread_create(&p, nullptr, Productor, &bqs);
    pthread_create(&s, nullptr, Saver, &bqs);



    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    pthread_join(s, nullptr);


    delete bqs._cbq;
    delete bqs._sbq;


    //多生产多消费
    // BlockQueue<CalTask>* bq = new BlockQueue<CalTask>;
    // pthread_t p[4];
    // pthread_t c[3];

    // for(int i = 0; i < 4; ++i)
    // {
    //     pthread_create(p + i, nullptr, Productor, bq);
    // }

    // for(int i = 0; i < 3; ++i)
    // {
    //     pthread_create(c + i, nullptr, Consumer, bq);
    // }
    
    // for(const auto& e : p)
    // {
    //     pthread_join(e, nullptr);
    // }


    // for(const auto& e : c)
    // {
    //     pthread_join(e, nullptr);
    // }


    return 0;
}